import 'package:animated_digit/animated_digit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_routes.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/main/main_logic.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/widgets/button/debounce_gesture_detector.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../../config/app_color.dart';
import '../home_logic.dart';

class HomePrizePool extends StatelessWidget {
  const HomePrizePool({super.key, required this.logic});

  final HomeLogic logic;

  @override
  Widget build(BuildContext context) {
    // final HomeLogic logic = Get.find<HomeLogic>();
    return Container(
      padding: EdgeInsets.all(16.w).copyWith(bottom: 24.w),
      decoration: BoxDecoration(
        color: AppColor.navBgColor,
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(30.w),
          bottomRight: Radius.circular(30.w),
        ),
      ),
      child: Column(
        children: [
          // 标题区域
          _buildHeader(logic),

          16.verticalSpaceFromWidth,

          // 奖池数据区域
          _buildPrizePoolData(logic),

          12.verticalSpaceFromWidth,
          _buildBtns(),
        ],
      ),
    );
  }

  Widget _buildHeader(HomeLogic logic) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        // 左侧标题
        Row(
          children: [
            // 指示器圆点
            Container(
              width: 14.w,
              height: 14.w,
              decoration: BoxDecoration(
                color: AppColor.homePrizePoolBgIndicator.withOpacity(0.12),
                shape: BoxShape.circle,
              ),
              child: Center(
                child: Container(
                  width: 7.w,
                  height: 7.w,
                  decoration: const BoxDecoration(
                    color: AppColor.homePrizePoolIndicator,
                    shape: BoxShape.circle,
                  ),
                ),
              ),
            ),

            8.horizontalSpace,

            Text(
              LocaleKeys.jackpotAmount.tr,
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.w600,
                color: AppColor.textPrimary.withOpacity(0.94),
              ),
            ),
          ],
        ),

        // 右侧历史返奖信息
        Row(
          children: [
            Text(
              LocaleKeys.historyBonus.tr,
              style: TextStyle(
                fontSize: 12.sp,
                color: AppColor.textPrimary.withOpacity(0.7),
              ),
            ),
            2.horizontalSpace,
            Obx(() {
              return Text(
                logic.poolCountRx.value.formatHomePool(),
                style: TextStyle(
                  fontSize: 12.sp,
                  fontWeight: FontWeight.w600,
                  color: AppColor.primary,
                  letterSpacing: -0.38,
                ),
              );
            }),
            1.horizontalSpace,
            Text(
              LocaleKeys.times.tr,
              style: TextStyle(
                fontSize: 12.sp,
                color: AppColor.textPrimary.withOpacity(0.7),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildPrizePoolData(HomeLogic logic) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: 12.w),
      child: Row(
        children: [
          // USDT奖池
          Expanded(
            child: Obx(() => _buildPrizePoolItem(
                  amount: logic
                          .prizePoolRx.value.reservePrizePoolForUSDTConfigKey ??
                      0,
                  // ?.formatAmount() ??
                  // '',
                  currency: '${LocaleKeys.backup.tr}USDT',
                  label: LocaleKeys.jackpot.tr,
                )),
          ),

          8.horizontalSpace,

          // TRX奖池
          Expanded(
            child: Obx(() => _buildPrizePoolItem(
                  amount: logic.prizePoolRx.value.prizePoolForTRXConfigKey ?? 0,
                  // ?.formatAmount() ??
                  // '',
                  currency: 'TRX',
                  label: LocaleKeys.jackpot.tr,
                )),
          ),

          8.horizontalSpace,

          // 备用奖池
          Expanded(
            child: Obx(() => _buildPrizePoolItem(
                  amount: (logic
                          .prizePoolRx.value.reservePrizePoolForTRXConfigKey ??
                      0),
                  // .formatAmount(),
                  currency: '${LocaleKeys.backup.tr}TRX',
                  label: LocaleKeys.jackpot.tr,
                )),
          ),
        ],
      ),
    );
  }

  Widget _buildPrizePoolItem({
    required int amount,
    required String currency,
    required String label,
  }) {
    return Column(
      children: [
        // 金额
        AnimatedDigitWidget(
          value: amount / 1000000.0,
          textStyle: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w700,
            color: AppColor.primary,
            letterSpacing: -0.38,
          ),
          fractionDigits: 2,
          enableSeparator: true,
          firstScrollAnimate: true,
        ),

        8.verticalSpaceFromWidth,

        // 货币和标签
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              currency,
              style: TextStyle(
                fontSize: 12.sp,
                color: AppColor.textPrimary.withOpacity(0.7),
              ),
            ),
            // 2.horizontalSpace,
            Text(
              label,
              style: TextStyle(
                fontSize: 12.sp,
                color: AppColor.textPrimary.withOpacity(0.7),
              ),
            ),
          ],
        ),
      ],
    );
  }

  Widget _buildBtns() {
    return Row(
      children: [
        // 充值按钮
        Expanded(
          child: _buildFunctionButton(
            icon: Assets.homeBtn1,
            label: LocaleKeys.recharge.tr,
            gradientColors: AppColor.homeDepositGradient,
            onTap: () => _handleDeposit(),
          ),
        ),

        6.horizontalSpace,

        // 提现按钮
        Expanded(
          child: _buildFunctionButton(
            icon: Assets.homeBtn2,
            label: LocaleKeys.withdraw.tr,
            gradientColors: AppColor.homeWithdrawGradient,
            onTap: () => _handleWithdraw(),
          ),
        ),

        6.horizontalSpace,

        // 下载按钮
        Expanded(
          child: _buildFunctionButton(
            icon: Assets.homeBtn3,
            label: LocaleKeys.download.tr,
            gradientColors: AppColor.homeDownloadGradient,
            onTap: () => _handleDownload(),
          ),
        ),

        6.horizontalSpace,

        // 课程按钮
        Expanded(
          child: _buildFunctionButton(
            icon: Assets.homeBtn4,
            label: LocaleKeys.course.tr,
            gradientColors: AppColor.homeCourseGradient,
            onTap: () => _handleCourse(),
          ),
        ),
      ],
    );
  }

  Widget _buildFunctionButton({
    required String icon,
    required String label,
    required List<Color> gradientColors,
    required VoidCallback onTap,
  }) {
    return DebounceGestureDetector(
      onTap: onTap,
      child: Container(
        height: 97.w,
        width: 81.w,
        // color: Colors.red,
        decoration: BoxDecoration(
          gradient: LinearGradient(
            colors: gradientColors,
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
          ),
          borderRadius: BorderRadius.circular(8.w),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 图标
            SizedBox(
              width: 44.w,
              height: 44.w,
              child: AutoImage(
                icon,
                width: 44.w,
                height: 44.w,
              ),
            ),

            12.verticalSpaceFromWidth,

            // 标签
            Text(
              label,
              style: TextStyle(
                fontSize: 12.sp,
                fontWeight: FontWeight.w500,
                color: AppColor.textPrimary.withOpacity(0.94),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void _handleDeposit() {
    if (!AppUserManager().isLogin()) {
      Get.toNamed(AppRoutes.loginPage);
      return;
    }
    Get.find<MainLogic>().changeIndex(2);
  }

  void _handleWithdraw() {
    if (!AppUserManager().isLogin()) {
      Get.toNamed(AppRoutes.loginPage);
      return;
    }
    Get.toNamed(AppRoutes.promotionWithdrawPage)?.then((v) {
      logic.loadNoticeData();
    });
    ;
  }

  void _handleDownload() {
    if (!AppUserManager().isLogin()) {
      Get.toNamed(AppRoutes.loginPage);
      return;
    }
    // TODO: 实现下载逻辑
    Get.toNamed(AppRoutes.homeDownloadCenterPage)?.then((v) {
      logic.loadNoticeData();
    });
    ;
    // Get.snackbar('下载', '功能开发中...');
  }

  void _handleCourse() {
    if (!AppUserManager().isLogin()) {
      Get.toNamed(AppRoutes.loginPage);
      return;
    }
    Get.toNamed(AppRoutes.homeCoursePage)?.then((v) {
      logic.loadNoticeData();
    });
    ;
  }
}
